/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.service.loader.internal;

import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.jrose.api.annotation.common.ConfigurationLocation;
import org.jrose.api.annotation.remote.ExposeHttpService;
import org.jrose.core.util.ConversionUtils;
import org.jrose.core.util.XMLUtils;
import org.jrose.entity.config.Career;
import org.jrose.entity.config.CareerGroup;
import org.jrose.service.loader.AbstractStandardConfigurationLoader;
import org.jrose.service.loader.CareerGroupLoader;
import org.jrose.service.loader.CareerLoader;
import org.w3c.dom.Element;

import javax.inject.Named;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Default {@link CareerGroupLoader} implementation.
 *
 * @author Xiaohai Zhang
 * @see CareerLoader
 * @since 2013-08-29 20:40
 */
@Named
@NoArgsConstructor
@ConfigurationLocation("/config/career-group-configuration.xml")
@AbstractStandardConfigurationLoader.StandardConfiguration("career-group")
@ExposeHttpService(name = "/careerGroupLoader", serviceInterface = CareerGroupLoader.class)
public class CareerGroupLoaderImpl extends AbstractStandardConfigurationLoader<CareerGroup> implements CareerGroupLoader {
    private Map<Integer, CareerGroup> careerGroups = new LinkedHashMap<>();

    @Override
    protected CareerGroup parseElement(Element element) {
        int id = ConversionUtils.toInt(XMLUtils.getChildElementText(element, "id"));
        String name = StringUtils.defaultString(XMLUtils.getChildElementText(element, "name"));
        String description = StringUtils.defaultString(XMLUtils.getChildElementText(element, "description"));
        String imgUrl = StringUtils.defaultString(XMLUtils.getChildElementText(element, "imgUrl"));
        CareerGroup careerGroup = new CareerGroup();
        careerGroup.setId(id);
        careerGroup.setName(name);
        careerGroup.setDescription(description);
        careerGroup.setImgUrl(imgUrl);
        careerGroup.setCareers(new LinkedList<Career>());
        return careerGroup;
    }

    @Override
    protected void processConfigurations(List<CareerGroup> configurations) {
        CareerLoader careerLoader = getBean(CareerLoader.class);
        for (CareerGroup careerGroup : configurations) {
            Validate.isTrue(!careerGroups.containsKey(careerGroup.getId()), "Duplicated career group %s found", careerGroup.getId());
            careerGroups.put(careerGroup.getId(), careerGroup);
            List<Career> careers = careerLoader.loadByGroupId(careerGroup.getId());
            if (careers != null) {
                careerGroup.getCareers().addAll(careers);
            }
        }
    }

    @Override
    public CareerGroup load(int id) {
        initializeLoader();
        return careerGroups.get(id);
    }
}
